//
//  BuyBooksViewController.m
//  Wy_BuyBooks
//
//  Created by wangyang on 2020/2/6.
//  Copyright © 2020 wangyang. All rights reserved.
//

#import "BuyBooksViewController.h"
#import "WyLnadViewController.h"
@interface BuyBooksViewController ()<UITextViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong) UIScrollView   *WyScrollView;
@property (nonatomic,strong) UITextView    *WyTextView;

@end

@implementation BuyBooksViewController
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if ([WyEngineering sharedUser].login == YES) {

          [self WyScrollView];
        [self initSetIU];

       }else{

           //                [self.MyTableView removeFromSuperview];
           //                self.MyTableView = nil;
           WyLnadViewController *VC = [[WyLnadViewController alloc] init];
           VC.LandVcBlock = ^{
               //                        [self.MyTableView removeFromSuperview];
               //                        self.MyTableView = nil;
               self.tabBarController.selectedIndex = 0;
           };
           [self.navigationController pushViewController:VC animated:YES];
       }
}
-(void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self WyScrollView];
    [self initSetIU];
    if (@available(iOS 11.0, *)) {
        
        self.WyScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
}



-(void)initSetIU{
    
    self.SellingBooksLabel = [UILabel labelWithContent:@"卖书须知:" superView:self.WyScrollView textColor:WyBlackColor font:WySYSTEMFONT(16) textAlignment:NSTextAlignmentLeft];
    [ self.SellingBooksLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(CGSizeMake(BiLiWidth(200), BiLiWidth(20)));
        make.left.mas_offset(BiLiWidth(15));
        make.top.equalTo(self.WyTextView.bottom).offset(BiLiWidth(10));
    }];
    
    
    NSArray *array = @[@"不收有影响阅读的书籍（如破损、老化、污渍等）。",@"不收盗版、非卖品、非法书籍",@"不收教育书籍（小学到高中教科书）。"];
    
    for (int i = 0; i<array.count; i++) {
        UIImageView *img = [UIImageView imageWithImgName:@"点" superView:self.WyScrollView];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(BiLiWidth(11), BiLiWidth(11)));
            make.top.equalTo(self.SellingBooksLabel.bottom).offset(BiLiWidth(13)+BiLiWidth(13+16)*i);
            make.left.mas_offset(BiLiWidth(15));
        }];
        
        UILabel *labe = [UILabel labelWithContent:array[i] superView:self.WyScrollView textColor:UIColorFromRGB(0x999999) font:WySYSTEMFONT(14) textAlignment:NSTextAlignmentLeft];
        [labe mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(BiLiWidth(300), BiLiWidth(20)));
            make.top.equalTo(self.SellingBooksLabel.bottom).offset(BiLiWidth(11)+BiLiWidth(13+16)*i);
            make.left.equalTo(img.right).offset(BiLiWidth(5));
        }];
    }
    
}

- (UIScrollView *)WyScrollView {
    
    if (!_WyScrollView) {
        _WyScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:_WyScrollView];
        _WyScrollView.userInteractionEnabled = YES;
        _WyScrollView.showsVerticalScrollIndicator = NO;
        _WyScrollView.contentSize = CGSizeMake(WySCREENWIDTH, WySCREENHEIGHT*1.5);
        [_WyScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_offset(0);
        }];
        
        [self WyTextView];
       
    }
    return _WyScrollView;
}

- (UITextView *)WyTextView

{
    if (!_WyTextView) {
        
        _WyTextView = [[UITextView alloc]init];
        [self.WyScrollView addSubview:_WyTextView];
        _WyTextView.delegate = self;
        _WyTextView.text = @"请概述书籍简介及作品内容";
        _WyTextView.textColor  = [UIColor grayColor];
        _WyTextView.editable = YES;
        _WyTextView.backgroundColor = [UIColor whiteColor];
        WyViewRadius(_WyTextView, BiLiWidth(10));
        _WyTextView.font = [UIFont systemFontOfSize:BiLiWidth(14)];
        [_WyTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(WySCREENWIDTH-BiLiWidth(30), BiLiWidth(160)));
            make.top.mas_offset(BiLiWidth(10));
            make.centerX.mas_offset(0);
        }];
        [self WycameImg];
    }
    return _WyTextView;
}

- (UIImageView *)WycameImg

{
    if (!_WycameImg) {
        
        _WycameImg = [UIImageView imageWithImgName:@"xiangji" superView:self.WyTextView];
        [_WycameImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(CGSizeMake(BiLiWidth(60), BiLiWidth(60)));
            make.top.mas_offset(BiLiWidth(80));
            make.left.equalTo(BiLiWidth(15));
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapselector)];
        _WycameImg.userInteractionEnabled = YES;
        [_WycameImg addGestureRecognizer:tap];
    }
    return _WycameImg;
}
-(void)tapselector
{
    UIActionSheet* actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
    [actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
                UIImagePickerController* imgPicker = [[UIImagePickerController alloc]init];
                imgPicker.delegate = self;
                imgPicker.allowsEditing = YES;
                imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:imgPicker animated:YES completion:nil];
            }
            else
            {
                UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"信息提示" message:@"相机不能工作" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
        }
            break;
        case 1:
        {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
            {
                UIImagePickerController* imgPicker2 = [[UIImagePickerController alloc]init];
                imgPicker2.delegate = self;
                imgPicker2.allowsEditing = YES;
                //imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//默认值相册
                [self presentViewController:imgPicker2 animated:YES completion:nil];
            }
            else
            {
                UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"信息提示" message:@"相册不能使用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
            }
        }
            break;
        default:
            break;
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    _WycameImg.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    UIImageWriteToSavedPhotosAlbum(_WycameImg.image, nil, nil, nil);
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (UILabel *)SellingBooksLabel

{
    if (!_SellingBooksLabel) {
        _SellingBooksLabel = [[UILabel alloc]init];
    }
    return _SellingBooksLabel;
}

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([self.WyTextView.text isEqualToString:@"请概述书籍简介及作品内容"]) {
        self.WyTextView.text = @"";
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
